package hichecker.ui.custom_views.course;

import hichecker.controller.Config;
import hichecker.controller.MainController;
import hichecker.ui.components.verifier.verifier_text_field.AbstractVerifyAction;
import hichecker.ui.components.verifier.verifier_text_field.VerifierTextField;
import hichecker.ui.components.verifier.verifier_text_field.VerifierTextField.InputType;
import hichecker.ui.general_views.container_panel.content.ContentPanel;
import hichecker.university.Course;
import hichecker.university.UniversityPart;

import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;

public class CourseSettings extends ContentPanel {
	private MainController controller;
	private List<VerifierTextField> inputs;

	public CourseSettings(UniversityPart item, final MainController controller) {
		super(item);
		this.controller = controller;
		Course course = (Course) item;

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		inputs = new ArrayList<VerifierTextField>();

		inputs.add(new VerifierTextField(course.getName(), InputType.STRING, Config.COURSE_NAME,
				new AbstractVerifyAction() {
					@Override
					public void process(String value) {
						controller.setCourseName(value);
					}
				}));
		inputs.add(new VerifierTextField(course.getNumber(), InputType.NUMBER,
				Config.COURSE_NUMBER, new AbstractVerifyAction() {
					@Override
					public void process(String value) {
						controller.setCourseNumber(Integer.parseInt(value));
					}
				}));
		inputs.add(new VerifierTextField(course.getTerm(), InputType.SEMESTER, Config.COURSE_TERM,
				new AbstractVerifyAction() {
					@Override
					public void process(String value) {
						controller.setCourseTerm(value);
					}
				}));
		inputs.add(new VerifierTextField(course.getLecturer(), InputType.STRING,
				Config.COURSE_LECTURER, new AbstractVerifyAction() {
					@Override
					public void process(String value) {
						controller.setCourseLecturer(value);
					}
				}));
		inputs.add(new VerifierTextField(null, InputType.STRING, Config.COURSE_GROUPLEADER,
				new AbstractVerifyAction() {
					@Override
					public void process(String value) {
						// TODO add course groupleader
					}
				}));

		for (VerifierTextField input : inputs)
			add(input);
	}

	@Override
	public boolean isCorrect() {
		for (VerifierTextField input : inputs)
			if (!input.isCorrect())
				return false;
		return true;
	}

	@Override
	public ContentPanel switchViewSettings() {
		return new CourseView(item, controller);
	}
}
